package com.gopay.bis.service.bank.impl.tpas;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.gopay.bis.manager.bank.tpas.TpasOfpayManager;
import com.gopay.common.constants.bank.tpas.TpasMwaChargeConstants;
import com.gopay.common.domain.bank.tpas.TpasMessage;
import com.gopay.common.domain.bank.tpas.TpasResult;
import com.gopay.common.domain.bank.tpas.mwa.FlowMessage;
import com.gopay.common.domain.bank.tpas.mwa.MobileMessage;
import com.gopay.common.domain.bank.tpas.mwa.OilCardMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.TpasChargeInteractRemoteService;

public class TpasOfpayServiceImpl implements TpasChargeInteractRemoteService {
    @Resource(name="tpasOfpayManager") 
    private TpasOfpayManager tpasOfpayManager;
   
    @Override
    public TpasResult mobileBelong(TpasMessage base) throws GopayException {
        TpasResult res = tpasOfpayManager.mobileBelong(base);
        return res;
    }

    @Override
    public TpasResult mobileCharge(TpasMessage base) throws GopayException {
        MobileMessage msg = (MobileMessage)base;
        if(StringUtils.isBlank(msg.getMcType()))
            msg.setMcType("");
        TpasResult res = tpasOfpayManager.mobileCharge(base);
        return res;
    }

    @Override
    public TpasResult flowCharge(TpasMessage base) throws GopayException {
        FlowMessage msg = (FlowMessage)base;
        if(StringUtils.isBlank(msg.getNetType()))
            msg.setNetType("");
        TpasResult res = tpasOfpayManager.flowCharge(base);
        return res;
    }

    @Override
    public TpasResult oilCardInfo(TpasMessage base) throws GopayException {
        OilCardMessage msg = (OilCardMessage)base;
        if(StringUtils.isEmpty(msg.getCardType()))  //卡类型，如果不传，默认为1
            msg.setCardType("1");
        TpasResult res = tpasOfpayManager.oilCardInfo(base);
        return res;
    }
    
    @Override
    public TpasResult oilCardCharge(TpasMessage base) throws GopayException {
        OilCardMessage msg = (OilCardMessage)base;
        if(StringUtils.isEmpty(msg.getCardType()))  //卡类型，如果不传，默认为1
            msg.setCardType("1");
        if(StringUtils.isEmpty(msg.getCardNum()))  //充值数量，如果不传，默认为1
            msg.setCardNum("1");
        if(StringUtils.isEmpty(msg.getUsername()))  
            msg.setUsername("");
        if(StringUtils.isEmpty(msg.getInvoiceFlag()))  
            msg.setInvoiceFlag("1");
        TpasResult res = tpasOfpayManager.oilCardCharge(base);
        return res;
    }

    @Override
    public TpasResult queryOrderStatus(TpasMessage base) throws GopayException {
        TpasResult res = tpasOfpayManager.queryOrderStatus(base);
        return res;
    }

    @Override
    public TpasResult tpasBack(TpasMessage msg) throws GopayException {
        TpasResult result = new TpasResult(true);
        TpasMessage res = new MobileMessage();
        String retCode = msg.getRetCode();
        if("1".equals(retCode)){
            result.setOrderStatus(true);
            res.setRetCode(TpasMwaChargeConstants.CHARGE_SUCCESS);
        }else if("9".equals(retCode)){
            result.setOrderStatus(false);
            res.setRetCode(TpasMwaChargeConstants.CHARGE_FAIL);
        }
        result.setDefaultModel(res);
        return result;
    }
}
